package util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class AesUtil {

    private static final String ALGORITHM_AES = "AES";

    private static final String CHARSET_UTF8 = "utf-8";

    /**
     * 加密
     * 
     * @param encodeRules
     * @param content
     * @return
     */
    public static String encodeAes(String encodeRules, String content) {

        try {
            SecretKey key = new SecretKeySpec(encodeRules.getBytes(CHARSET_UTF8), ALGORITHM_AES);
            //1.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance(ALGORITHM_AES);
            //2.初始化密码器，第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作，第二个参数为使用的KEY
            cipher.init(Cipher.ENCRYPT_MODE, key);
            //3.获取加密内容的字节数组(这里要设置为utf-8)不然内容中如果有中文和英文混合中文就会解密为乱码
            byte[] byteEncode = content.getBytes(CHARSET_UTF8);
            //4.根据密码器的初始化方式--加密：将数据加密
            byte[] byteAes = cipher.doFinal(byteEncode);
            //5 配合mysql转16进制
            String encodeStr = Hex.encodeHexString(byteAes).toUpperCase();
            //6.将字符串返回
            return encodeStr;
        } catch (NoSuchAlgorithmException e) {
            System.out.println("eroor,e");
        } catch (NoSuchPaddingException e) {
            System.out.println("eroor,e");
        } catch (InvalidKeyException e) {
            System.out.println("eroor,e");
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        }catch (BadPaddingException e) {
            System.out.println("eroor,e");
        } catch (UnsupportedEncodingException e) {
            System.out.println("eroor,e");
        }
        //如果有错就返加nulll
        return null;
    }

    /**
     * 解密
     * 
     * @param encodeRules
     * @param content
     * @return
     */
    public static String decryptAes(String encodeRules, String content) {

        try {
            SecretKey key = new SecretKeySpec(encodeRules.getBytes(CHARSET_UTF8), ALGORITHM_AES);
            // 1.根据指定算法AES自成密码器
            Cipher cipher = Cipher.getInstance(ALGORITHM_AES);
            // 2.初始化密码器，第一个参数为加密(Encrypt_mode)或者解密(Decrypt_mode)操作，第二个参数为使用的KEY
            cipher.init(Cipher.DECRYPT_MODE, key);
            // 3.将加密并编码后的内容解码成字节数组
            byte[] byte_content;
            byte_content = Hex.decodeHex(content.toCharArray());
            // 4.解密
            byte[] byte_decode = cipher.doFinal(byte_content);
            String AES_decode = new String(byte_decode, CHARSET_UTF8);
            return AES_decode;
        } catch (NoSuchAlgorithmException e) {
            System.out.println("eroor,e");
        } catch (NoSuchPaddingException e) {
            System.out.println("eroor,e");
        } catch (InvalidKeyException e) {
            System.out.println("eroor,e");
        } catch (IOException e) {
            System.out.println("eroor,e");
        } catch (IllegalBlockSizeException e) {
            System.out.println("eroor,e");
        } catch (BadPaddingException e) {
            System.out.println("eroor,e");
        } catch (DecoderException e) {
            System.out.println("eroor,e");
//            log.error("error",e);
        }
        //如果有错就返加nulll
        return null;
    }

    public static void test(String content) {

        String ruls = "zytc-science-pro";
//        String content = "6225211680330858";
//        System.out.println(Hex.encodeHexString(content.getBytes()));
        String mima = encodeAes(ruls, content);
        System.out.println("原文是:" + content);
        System.out.println("根据输入的规则" + ruls + "加密后的密文是:" + mima);
//        String mimaaa = "DCCD32DA3863B8A47148583E07169B5B";
        System.out.println("根据输入的规则" + ruls + "解密后的明文是:" + decryptAes(ruls, mima));
    }
//
    public static void main(String[] args) {
        test("6225211680330858");
//        test("6217921670302333");
//        test("370283198409057013");
//        test("370785198805284891");
    }

}